// 自定义方法
function lxw() {
    // 表单转json
    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
    // js.css外加载
    window.dynamicLoading = {
        css: function (path) {
            if (!path || path.length === 0) {
                throw new Error('argument "path" is required !');
            }
            var head = document.getElementsByTagName('head')[0];
            var link = document.createElement('link');
            link.href = path;
            link.rel = 'stylesheet';
            link.type = 'text/css';
            head.appendChild(link);
        },
        js: function (path, callback) {
            if (!path || path.length === 0) {
                throw new Error('argument "path" is required !');
            }
            var head = document.getElementsByTagName('head')[0];
            var script = document.createElement('script');
            script.src = path;
            script.type = 'text/javascript';
            head.appendChild(script);
            if (callback !== undefined) {
                if (script.readyState) { //IE
                    script.onreadystatechange = function () {
                        if (script.readyState == "loaded" || script.readyState == "complete") {
                            script.onreadystatechange = null;
                            callback();
                        }
                    };
                } else { //Others
                    script.onload = function () {
                        callback();
                    };
                }
            }
        }
    }

    window.GetUrlArgsObj = function () {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            const str = url.substr(1);
            const strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

    // 随机5位数验证码
    window.createCode = function () {
        var code = "";
        var codeLength = 5; //验证码的长度
        var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //随机数
        for (var i = 0; i < codeLength; i++) { //循环操作
            var charIndex = Math.floor(Math.random() * 36); //取得随机数的索引
            code += random[charIndex]; //根据索引取得随机数加到code上
        }
        return code; //把code值赋给验证码
    }


}
lxw();


function fun() {
    // 默认环境
    var env = "https://official" //这个地方不要动
    var nowCurrentHost = location.hostname;
    if (nowCurrentHost === 'localhost' || nowCurrentHost === '' || nowCurrentHost === '192.168.1.3') {
        var env = "http://dev"
        var env = "http://test"
        // var env = "https://uat-official.jtexpress.com.cn"
        // var env = "https://official.jtexpress.com.cn"


    } else if (nowCurrentHost === "prewww.jtexpress.com.cn") {
        var env = "http://dev"
        var env = "http://test"
    }
    // post
    window.POSTpageNetwork = env + "" //[网点查询-网点搜索]
    window.GETpageArticleType2 = env + "" //[在线下单-物品类型]


    window.sendata = function (url, data, headers) {
        headers === undefined ? headers = {} : headers = headers;
        (headers.request !== undefined) ? headers.request = headers.request : headers.request = "POST";
        (headers.contentType !== undefined) ? headers.contentType = headers.contentType : headers.contentType = "application/x-www-form-urlencoded; charset=UTF-8";
        // (headers.request === "POST") ? layer.load() : ""
        var _result = ''
        $.ajax({
            type: headers.request,
            url: url,
            async: false,
            contentType: headers.contentType,
            data: data,
            error: function error() {
                // layer.closeAll('loading');
                return false;
            },
            success: function success(json) {
                // layer.closeAll('loading');
                if (typeof json === "object") {
                    _result = json;
                } else {
                    _result = $.parseJSON(json);
                }
            }
        }, "json");
        return _result;
    }

    // window.onerror=function(){return true;}
}
fun();

window.onload = function () {
    //加载时间
    var loadTime = window.performance.timing.domContentLoadedEventEnd - window.performance.timing.navigationStart;
    console.log('Page load time is ' + loadTime);
}
